package com.java2007.controller.backend;

import com.java2007.controller.BaseServlet;
import com.java2007.factory.BeanFactory;
import com.java2007.pojo.FoodType;
import com.java2007.service.FoodTypeService;
import com.java2007.service.impl.FoodTypeServiceImpl;
import org.apache.commons.beanutils.BeanUtils;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 菜类别控制器
 * @author ZOU
 */
@WebServlet("/type")
public class FoodTypeController extends BaseServlet {

//    FoodTypeServiceImpl foodTypeService = new FoodTypeServiceImpl();
    private FoodTypeService foodTypeService = (FoodTypeService) BeanFactory.getBean("com.java2007.service.impl.FoodTypeServiceImpl");

    /**
     * 查询
     *
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public String search(HttpServletRequest request, HttpServletResponse response) throws Exception {
            //获取请求


        String keyword = request.getParameter("keyword");
            //条件存入域对象，作为回显值
            request.setAttribute("keyword", keyword);
            //调用相应方法
            List<FoodType> types = this.foodTypeService.findByFoodTypeName(keyword);
            //结果存入域对象
            request.setAttribute("types", types);
            //转发
            return ("forward:/backend/detail/foodtype/food-type-list.jsp");

    }


    /**
     * 通过ID进行查询
     *
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public String byId(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //获取请求
        String strId = request.getParameter("typeId");
        //传入的ID不是int类型，转型
        int typeId = Integer.parseInt(strId);
        //调用相应方法
        FoodType foodType = foodTypeService.findByFoodTypeId(typeId);
        //存入域对象
        request.setAttribute("type", foodType);
        //转发
        return ("forward:/backend/detail/foodtype/food-type-update.jsp");

    }

    /**
     * 增加
     *
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public String save(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //获取请求
        String typeName = request.getParameter("typeName");
        //调用相应方法
        foodTypeService.saveByFoodTypeName(typeName);
        //重定向
        return ("redirect:/type?method=search");
    }

    /**
     * 通过ID删除
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public String delete(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //获取请求
        String idStr = request.getParameter("id");
        //传入的ID不是int类型，转型
        int typeId = Integer.parseInt(idStr);
        //调用相应方法
        foodTypeService.deleteByFoodTypeId(typeId);
        //重定向
        return ("redirect:/type?method=search");


    }

    /**
     * 通过ID更新
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public String update(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //获取请求
        Map<String, String[]> parameterMap = request.getParameterMap();
        System.out.println(parameterMap);
        //新建foodType对象
        FoodType foodType = new FoodType();
        //将parameterMap对象封装进foodType对象中（因为更新值是通过ID更新，并且需要更新后的数据传递，所以使用map对象）
        BeanUtils.populate(foodType, parameterMap);
        //调用相应方法
        foodTypeService.updata(foodType);
        //重定向
        return ("forward:/type?method=search");

    }
}
